<?php
/**
* 我的收藏
*/
class Collection extends CS_Controller
{
    
    public function _init()
    {
        $this->load->model('scenery_enshrine_model', 'scenery_enshrine');
        $this->load->model('tourism_enshrine_model', 'tourism_enshrine');
    }

    /**
     * 景区收藏
     */
    public function sceneryCollectionList()
    {
        $count = 20;
        $total = $this->scenery_enshrine->sceneryEnshrineTotal(array('uid' => $this->uid));
        if ( $total > 0 ) {
            $this->data['collectionList'] = $this->scenery_enshrine->sceneryEnshrineList(array('uid' => $this->uid), 0, $count);
        }
        $this->data['more'] = $total > $count ? true : false;
        $this->load->view('home/collection/sceneryCollectionList', $this->data);
    }

    /**
     * ajax 景区刷新列表
     */
    public function ajaxSceneryCollection()
    {
        $page = $this->input->post('page');
        $num = 20;
        $total = $this->scenery_enshrine->sceneryEnshrineTotal(array('uid' => $this->uid));
        $data['collectionList'] = $this->scenery_enshrine->sceneryEnshrineList(array('uid' => $this->uid), $num * $page, $num);

        echo json_encode(
                array(
                    'status' => ($total >  ($page + 1) * $num) ? true : false,
                    'html' => $this->load->view('home/collection/ajaxSceneryCollection', $data, true)
                )
            );
        exit;
    }

    /**
     * ajax景区收藏删除
     */
    public function ajaxSceneryDelete()
    {
        $ids = explode(',', rtrim($this->input->post('id'), ','));

        $status = true;
        if ( ! empty($ids) )
        {
            $status = $this->scenery_enshrine->deleteEnshrine($ids);
        }
        echo json_encode(
                array(
                    'status'   => $status,
                    'url' => site_url('home/collection/sceneryCollectionList')
                )
            );
        exit;
    }

    /*
     * 商品收藏
     */
    public function tourismCollectionList()
    {
        $count = 20;
        $total = $this->tourism_enshrine->tourismEnshrineTotal(array('uid' => $this->uid));
        if ( $total > 0 ) {
            $this->data['collectionList'] = $this->tourism_enshrine->tourismEnshrineList(array('uid' => $this->uid), 0, $count);
        }
        $this->data['more'] = $total > $count ? true : false;
        $this->load->view('home/collection/tourismCollectionList', $this->data);
    }

    /**
     * ajax 刷新列表
     */
    public function ajaxTourismCollection()
    {
        $page = $this->input->post('page');
        $num = 20;
        $total = $this->tourism_enshrine->tourismEnshrineTotal(array('uid' => $this->uid));
        $data['collectionList'] = $this->tourism_enshrine->tourismEnshrineList(array('uid' => $this->uid), $num * $page, $num);

        echo json_encode(
                array(
                    'status' => ($total >  ($page + 1) * $num) ? true : false,
                    'html' => $this->load->view('home/collection/ajaxTourismCollection', $data, true)
                )
            );
        exit;
    }

    /**
     * ajax景区收藏删除
     */
    public function ajaxTourismDelete()
    {
        $ids = explode(',', rtrim($this->input->post('id'), ','));

        $status = false;
        if ( ! empty($ids) )
        {
            $status = $this->tourism_enshrine->deleteEnshrine($ids);
        }
        echo json_encode(
                array(
                    'status' => $status,
                    'url' => site_url('home/collection/tourismCollectionList')
                )
            );
        exit;
    }

    /**
     * 酒店收藏
     */
    public function hotelCollectionList()
    {
        $total = 0;
        $num = 20;
        $param = array(
            'uid'  => $this->uid,
            'p'    => 1,
            'size' => $num
        );
        $result = json_decode(fn_get_contents($this->config->hotel_api_url . 'admin/OrderCenter/HotelCollection', $param, 'post'));
        if (empty($result)) show_404();
        if ($result->status === 0) {
            $this->data['hotelCollectionList'] = $result->data;
            $total = $result->data->count;
        }
        $data['more'] = $total > $num ? true : false;
        $this->load->view('home/collection/hotelCollectionList', $this->data);
    }

    /**
     * ajax 酒店收藏
     */
    public function ajaxHotelCollection()
    {
        $page = $this->input->post('page');
        $num = 20;
        $param = array(
            'uid'  => $this->uid,
            'p'    => $page,
            'size' => $num
        );
        $result = json_decode(fn_get_contents($this->config->hotel_api_url . 'admin/OrderCenter/HotelCollection', $param, 'post'));
        if (empty($result)) $this->jsonMessage('数据异常');
        if ($result->status = 'E000100') $total = 0;// 请求无数据
        if ($result->status === 0) {
            $data['hotelCollectionList'] = $result->data;
            $total = $result->data->count;
        }
        echo json_encode(
            array(
                'status' => ($total > ($page + 1) * $num) ? true : false,
                'html'   => $this->load->view('home/collection/ajaxHotelCollection', $data, true)
            )
        );
        exit;
    }

    /**
     * ajax 删除酒店收藏
     */
    public function ajaxHotelDelete()
    {
        $ids = rtrim($this->input->post('id'), ',');
        $status = false;
        if (!empty($ids)) {
            $param = array(
                'uid'      => $this->uid,
                'hotel_id' => $ids
            );
            $result = json_decode(fn_get_contents($this->config->hotel_api_url . 'admin/OrderCenter/RemoveCollection', $param, 'post'));
            if (empty($result)) show_404();
            if ($result->status !== 0) show_404($result->status);
            if ($result->status === 0) $status = true;
        }
        echo json_encode(
            array(
                'status' => $status,
                'url'    => site_url('home/collection/hotelCollectionList')
            )
        );
        exit;
    }

    /**
     * 酒店收藏
     */
    public function lineCollectionList()
    {
        $this->load->view('home/collection/lineCollectionList', $this->data);
    }
}